package client.comm.baoding.ui

import android.view.LayoutInflater
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import client.comm.commlib.toast.toast
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsOrder
import client.comm.baoding.api.bean.TuikuanSelectGoods
import client.comm.baoding.databinding.ActivityApplytuikuanBinding
import client.comm.baoding.databinding.LayoutTuikuangoodswrapitemBinding
import client.comm.baoding.dialog.SelectBacktypeDialog
import client.comm.baoding.ui.vm.ApplyTuikuanModel
import org.json.JSONObject

class ApplyTuiKuanActivity :
    BaseMiddleActivity<ApplyTuikuanModel, ActivityApplytuikuanBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_applytuikuan
    }

    val list = MutableLiveData<MutableList<TuikuanSelectGoods>>()
    override fun initViewModel(): ApplyTuikuanModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
    }

    override fun initData() {
        val bean = intent.getParcelableExtra<GoodsOrder.Order>("bean")

        bean?.let {
            binding.bean = it
            binding.itemWrap.removeAllViews()
            val list = ArrayList<TuikuanSelectGoods>()
            for (i in it.goods_list) {
                DataBindingUtil.inflate<LayoutTuikuangoodswrapitemBinding>(
                    LayoutInflater.from(this),
                    R.layout.layout_tuikuangoodswrapitem,
                    binding.itemWrap,
                    false
                ).apply {
                    mImgLoader.load(i.goods_img).into(img)
                    val bbb = TuikuanSelectGoods(
                        create_time = i.create_time,
                        goods_freight = i.goods_freight,
                        goods_id = i.goods_id,
                        goods_img = i.goods_img,
                        goods_name = i.goods_name,
                        goods_num = i.goods_num,
                        goods_status = i.goods_status,
                        id = i.id,
                        order_id = i.order_id,
                        order_goods_id = i.order_goods_id,
                        price = i.price,
                        status = i.status,
                        total_price = i.total_price,
                        isSelect = ObservableField<Boolean>(false)
                    )
                    this.bean = bbb
                    list.add(bbb)
                    root.setOnClickListener {
                        if (bbb.isSelect.get() == true) {
                            return@setOnClickListener
                        }
                        for (b in this@ApplyTuiKuanActivity.list.value!!) {
                            if (b.isSelect.get() == true) {
                                b.isSelect.set(false)
                            }
                        }
                        val b = true
                        bbb.isSelect.set(b)
                    }
                    binding.itemWrap.addView(root)
                }
            }
            this@ApplyTuiKuanActivity.list.value = list
        }
    }

    override fun observerUI() {
        viewModel.commitRet.observe(this, Observer {
            it?.let {
                if (it.isSuccess) {
                    toast("提交成功")
                    finish()
                } else {
                    toast("提交失败")
                }
            }
        })
    }

    fun applyOrder() {
//        if (viewModel.typeBack.value == 0) {
//            toast("请选择退款方式")
//            return
//        }
//        if (viewModel.typeBack.value == 1 && intent.getParcelableExtra<GoodsOrder.Order>("bean")!!.status != 1) {
//            toast("订单已发货，不能只退款")
//            return
//        }
        val obj = JSONObject()
//        obj.put("back_status", "${viewModel.typeBack.value}")
        obj.put("back_status", "1")
        if (viewModel.typeBack.value!! > 1) {
            if (viewModel.wuliuNum.value.isNullOrEmpty() || viewModel.wuliu.value.isNullOrEmpty()) {
                toast("请输入物流")
                return
            }
            obj.put("logistics_num", viewModel.wuliuNum.value!!)
            obj.put("logistics_name", viewModel.wuliu.value!!)
        } else {
            obj.put("logistics_num", "")
            obj.put("logistics_name", "")
        }
        var goodsId = ""
        for (b in list.value!!) {
            if (b.isSelect.get() == true) {
                goodsId = "${b.order_goods_id}"
                obj.put("num", "${b.goods_num}")

            }
        }
        if (goodsId.isNullOrEmpty()) {
            toast("请选择商品")
            return
        }

        obj.put("order_goods_id", goodsId)


        viewModel.commitOrder(obj)
    }

    fun selectType() {
        SelectBacktypeDialog(this).apply {
            one = {
                viewModel.typeBack.value = it
                when (it) {
                    1 -> {
                        viewModel.isShowWl.value = false
                        viewModel.typeTxt.value = "只退款"
                    }
                    2 -> {
                        viewModel.isShowWl.value = true
                        viewModel.typeTxt.value = "只退货"
                    }
                    3 -> {
                        viewModel.isShowWl.value = true
                        viewModel.typeTxt.value = "退货且退款"
                    }
                }
            }
        }.show()
    }
}